package com.breeze.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.breeze.domain.User;
import com.breeze.handler.UserBlockHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
// 自动刷新配置文件
@RefreshScope
public class UserController {

    @Value("${text}")
    private String text;

    @GetMapping("/getById/{id}")
    /*@SentinelResource(value = "user",blockHandler = "getByIdBlockHandler",blockHandlerClass = UserBlockHandler.class
     ,fallback = "getByIdFallback")*/
    @SentinelResource(value = "user",blockHandler = "getByIdBlockHandler")
    public User getById(@PathVariable Long id){
        return User.builder().id(id).name("zs").intro(text).build();
    }

    public User getByIdFallback(Long id){
        return User.builder().id(id).name("我降级了！").intro(text).build();
    }

    public User getByIdBlockHandler(Long id, BlockException exception){
        return User.builder().id(-2L).intro("我降级了！").build();
    }

}
